package com.teacher.pc.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.teacher.common.CommonResult;
import com.teacher.mapper.TermMapper;
import com.teacher.pojo.entity.StaticData;
import com.teacher.service.IStaticDataService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 静态数据 前端控制器
 * </p>
 *
 * @author zfm
 * @since 2021-08-29
 */
@RestController
@RequestMapping("/pc/static-data")
public class StaticDataController {

    @Autowired
    private IStaticDataService iStaticDataService;

    @Autowired
    private TermMapper termMapper;

    /**
     * 生成本学期数据
     *
     * @return
     */
    @ApiOperation("生成本学期数据 staticData_create")
    @PostMapping
    @PreAuthorize("hasAuthority('staticData_create')")
    public CommonResult<String> create() {
        Long termId = termMapper.getCurrentTerm();
        if (termId == null) {
            return CommonResult.failed("当前无默认学期");
        }
        iStaticDataService.create();
        return CommonResult.success("提交请求成功，请耐心等待");
    }

    /**
     * 查询历史数据
     *
     * @param termId
     * @param classId
     * @return
     */
    @ApiOperation("查询历史数据 staticData_get")
    @GetMapping
    @PreAuthorize("hasAuthority('staticData_get')")
    public CommonResult<List<StaticData>> getHistoryData(Long termId, Long classId) {
        List<StaticData> staticDataList = iStaticDataService.list(new QueryWrapper<StaticData>()
                .lambda()
                .eq(StaticData::getTermId, termId)
                .eq(StaticData::getClassId, classId));
        return CommonResult.success(staticDataList);
    }

}

